package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理类")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;


    @PostMapping("/list")
    @ApiOperation(value = "资源列表查询")
    @ApiOperationSupport(includeParameters = {"resourceDto.resourceType","resourceDto.parentResourceNo"})
    public ResponseResult<List<ResourceVo>> selectList(@RequestBody ResourceDto resourceDto){
        List<ResourceVo> list = resourceService.selectList(resourceDto);
        return success(list);
    }

    @PostMapping("/tree")
    @ApiOperation(value = "获取资源树形表")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    public ResponseResult<TreeVo> selectToTree(@RequestBody ResourceDto resourceDto){
        TreeVo treeVo = resourceService.selectToTree(resourceDto);
        return success(treeVo);
    }

    @PutMapping
    @ApiOperation(value = "资源添加")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState","resourceDto.parentResourceNo","resourceDto.icon",
            "resourceDto.requestPath","resourceDto.resourceName","resourceDto.resourceType","resourceDto.sortNo"})
    public ResponseResult addResource(@RequestBody ResourceDto resourceDto){
        resourceService.addResource(resourceDto);
        return success();
    }

    @PatchMapping
    @ApiOperation(value = "资源修改")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState","resourceDto.parentResourceNo","resourceDto.icon",
            "resourceDto.requestPath","resourceDto.resourceName","resourceDto.resourceType","resourceDto.sortNo","resourceDto.id"})
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto){
        resourceService.updateResource(resourceDto);
        return success();
    }

    @PostMapping("/enable")
    @ApiOperation(value = "启用/禁用接口")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState","resourceDto.parentResourceNo","resourceDto.resourceNo"})
    public ResponseResult updateDataStatus(@RequestBody ResourceDto resourceDto){
        resourceService.updateDataStatus(resourceDto);
        return success();
    }

    @DeleteMapping("/{resourceNo}")
    @ApiOperation(value = "资源删除")
    public ResponseResult deleteResource(@PathVariable String resourceNo){
        resourceService.deleteResource(resourceNo);
        return success();
    }

    @GetMapping("/menus")
    @ApiOperation(value = "获取菜单")
    public ResponseResult<List<MenuVo>> getMenu(){
        List<MenuVo> menu = resourceService.getMenu();
        return success(menu);
    }

}
